package com.rain.cy.business.service;

import com.google.common.collect.Maps;
import com.rain.cy.business.constant.DicCategoryConstant;
import com.rain.cy.business.model.entity.DictionaryEntity;
import com.rain.cy.business.mapper.DictionaryMapper;
import com.rain.cy.framework.entity.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author yuchen
 * @since 2021-10-02
 */
@Service
public class DictionaryService extends BaseServiceImpl<DictionaryMapper, DictionaryEntity> {

    private static final Map<String, String> LOCAL_CACHE = Maps.newHashMap();

    public List<DictionaryEntity> getDrugType() {
        return getDictionaryByType(DicCategoryConstant.DRUG_TYPE);
    }


    public List<DictionaryEntity> getUserFeedBackTag() {
        return getDictionaryByType(DicCategoryConstant.USER_FEEDBACK_TAG);
    }

    public List<DictionaryEntity> getDepType() {
        return getDictionaryByType(DicCategoryConstant.DEP_TYPE);
    }

    public List<DictionaryEntity> getWorkType() {
        return getDictionaryByType(DicCategoryConstant.WORK_TYPE);
    }

    public List<DictionaryEntity> getCancerType() {
        return getDictionaryByType(DicCategoryConstant.CANCER_TYPE);
    }

    public List<DictionaryEntity> getDictionaryByType(String type) {
        return lambdaQuery().eq(DictionaryEntity::getCategory, type).list();
    }

    public String getDictionaryValueByType(String type, String key) {
        String mapKey = type + "-" + key;
        if (LOCAL_CACHE.get(mapKey) == null) {
            DictionaryEntity dictionaryEntity = lambdaQuery()
                    .eq(DictionaryEntity::getCategory, type)
                    .eq(DictionaryEntity::getItemKey, key)
                    .last("limit 1")
                    .one();
            LOCAL_CACHE.put(mapKey, dictionaryEntity.getItemValue());
        }
        return LOCAL_CACHE.get(mapKey);
    }

    public String getDictionaryKeyByType(String type, String value) {
        return lambdaQuery()
                .eq(DictionaryEntity::getCategory, type)
                .eq(DictionaryEntity::getItemValue, value)
                .last("limit 1")
                .one().getItemKey();
    }

}
